//
//  WZDayPlanCell.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/8/25.
//  Copyright © 2016年 wanzhao. All rights reserved.
//



#import "WZDayPlanCell.h"
#import "WZDayPlan.h"


@interface WZDayPlanCell ()

WeakProperty UIView *line;
WeakProperty UILabel *timeLabel;
WeakProperty UILabel *zhutiLabel;
@property (nonatomic, weak) UILabel *remindLabel;
@property (nonatomic, weak) UIImageView *imgView;

@end

@implementation WZDayPlanCell

+ (instancetype)dayPlanCellWithTableView:(UITableView *)tableView
{
    static NSString *dayPlanCellId = @"dayPlanCell";
    WZDayPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:dayPlanCellId];
    if (cell == nil) {
        cell = [[WZDayPlanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:dayPlanCellId];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubViews];
    }
    return self;
}


#pragma mark - 添加cell上的子控件

- (void)setUpSubViews {
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = RGBCOLOR(212, 212, 212);
    line.backgroundColor = GrayColor;
    line.alpha = 0.5;
    [self.contentView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    self.line = line;
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Schedule_clock"]];
    [imgView sizeToFit];
    [self.contentView addSubview:imgView];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(16);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    self.imgView = imgView;
    
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.backgroundColor = ClearColor;
    timeLabel.font = Font14;
    timeLabel.textColor = [UIColor grayColor];
    [timeLabel sizeToFit];
    [self.contentView addSubview:timeLabel];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        //            make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.rightMargin.equalTo(self.contentView.mas_rightMargin).offset(-15);
        make.centerY.equalTo(self.contentView.mas_centerY);
        //            make.trailing.equalTo(self.contentView.mas_trailing).offset(-15);
        
    }];
    self.timeLabel = timeLabel;
    
    
    UILabel *zhutiLabel = [[UILabel alloc] init];
    
    zhutiLabel.backgroundColor = ClearColor;
    zhutiLabel.font = Font14;
    [zhutiLabel sizeToFit];
    [self.contentView addSubview:zhutiLabel];
    [zhutiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.mas_right).offset(10);
        make.right.equalTo(timeLabel.mas_left).offset(-10);
        make.centerY.equalTo(imgView.mas_centerY);
    }];
    
    self.zhutiLabel = zhutiLabel;
    
    UILabel *remindLabel = [[UILabel alloc] init];
    remindLabel.backgroundColor = ClearColor;
    remindLabel.font = Font14;
    remindLabel.textColor = RedColor;
    //        remindLabel.text = [NSString stringWithFormat:@"[来自%@]", @"XXX"];
    [remindLabel sizeToFit];
    [self.contentView addSubview:remindLabel];
    [remindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhutiLabel.mas_right);
        make.centerY.equalTo(zhutiLabel.mas_centerY);
    }];
    self.remindLabel = remindLabel;
}


/**设置cell中的值*/
- (void)setDayPlay:(WZDayPlan *)dayPlay
{
    _dayPlay = dayPlay;
    
    self.zhutiLabel.text = dayPlay.title;
    self.timeLabel.text = dayPlay.startTime;
    
    if ([dayPlay.typeFlag isEqualToString:@"1"]) {
        
        self.imgView.image = [UIImage imageNamed:@"Schedule_clock"];
    } else {
        
        self.imgView.image = [UIImage imageNamed:@"Schedule_task"];
    }
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    self.line.frame = CGRectMake(0, self.height - 0.5f, self.width, 0.5f);
//    self.timeLabel.frame = CGRectMake(10.0f, 0, 100.0f, self.height);
//    self.zhutiLabel.frame = CGRectMake(CGRectGetMaxX(self.timeLabel.frame), 0, self.width - 10.0f - CGRectGetMaxX(self.timeLabel.frame), self.height);
    
//    [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.imgView.mas_right).offset(10);
//        make.right.equalTo(self.timeLabel.mas_left).offset(-10);
//        make.centerY.equalTo(self.imgView.mas_centerY);
//    }];
}




/**get TimeStamp Unit: sss , length = 13 */
//从1970年开始到现在经过了多少秒
- (NSString *)getTimeStamp {
    NSString *timeStamp;
    NSDate *fromdate=[NSDate date];
    timeStamp = [NSString stringWithFormat:@"%f",[fromdate timeIntervalSince1970]*1000];
    return timeStamp;
}

/**TimeStamp2NSDate */
//将时间戳转换成NSDate,转换的时间为格林尼治时间
- (NSDate *)changeSpToTime:(NSString*)spString {
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString doubleValue]/1000];
//    NSLog(@"%@",confromTimesp);
    
    return confromTimesp;
    
}

/**TimeStamp2NSDate  result = 北京时间 */
//将时间戳转换成NSDate,加上时区偏移。这个转换之后是北京时间
- (NSDate*)zoneChange:(NSString*)spString {
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString doubleValue]/1000];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
    NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
//    NSLog(@"%@",localeDate);
    return localeDate;
}

/**时间差*/
//比较给定NSDate与当前时间的时间差，返回相差的秒数
- (long)timeDifference:(NSDate *)date {
    NSDate *localeDate = [NSDate date];
    long difference = fabs([localeDate timeIntervalSinceDate:date]);
    return difference;
}

/**NSDate2NSString */
//将NSDate按yyyy-MM-dd HH:mm:ss格式时间输出
- (NSString*)nsdateToString:(NSDate *)date{
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* string=[dateFormat stringFromDate:date];
//    NSLog(@"%@",string);
    return string;
}

/**NSString2TimeStamp */
//将yyyy-MM-dd HH:mm:ss格式时间转换成时间戳
- (long)changeTimeToTimeSp:(NSString *)timeStr {
    long time;
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:timeStr];
    time= (long)[fromdate timeIntervalSince1970];
//    NSLog(@"%ld",time);
    return time;
}

/**get system NSDate */
//获取当前系统的yyyy-MM-dd HH:mm:ss格式时间
- (NSString *)getTime {
    NSDate *fromdate=[NSDate date];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* string=[dateFormat stringFromDate:fromdate];
    return string;
}

/**NowDat2DateFormatter*/
//将当前时间转化为年月日格式
- (NSString *)changeDate {
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags =  NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSInteger hour = [comps hour];
    NSInteger min = [comps minute];
    NSInteger sec = [comps second];
    
    NSString *string = [NSString stringWithFormat:@"%zd年%zd月%zd日%zd时%zd分%zd秒",year,month,day,hour,min,sec];
    
//    NSLog(@"%@",string);
    return string;
}


- (NSString *)dateStrWithDateFormatterStr:(NSString *)dateStr {
    
    NSArray *dateArray = [dateStr componentsSeparatedByString:@" "];

    return dateArray.firstObject;

}

- (NSString *)timeStrWithDateFormatterStr:(NSString *)dateStr {
    
    NSArray *dateArray = [dateStr componentsSeparatedByString:@" "];
    
    NSString *timeStr = dateArray.lastObject;
    
    return [timeStr substringWithRange:NSMakeRange(0, 5)];
}

@end
